package app1.rapidsquare.com.core;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;


/**
 * Created by Hikmat on 13.08.2014.
 */
public class ConnectionDetector extends BroadcastReceiver {

    private boolean _isConnectionStatus;
    private Context mContext;

    public ConnectionDetector() {
        this(null);
    }

    public ConnectionDetector(Context context) {
        if (context != null) {
            this.mContext = context;
            CheckConnectivityManager();
        }
    }

    public boolean getConnectingStatus() {
        return _isConnectionStatus;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        this.mContext = context;
        CheckConnectivityManager();
    }

    private void CheckConnectivityManager() {
        _isConnectionStatus = false;
        ConnectivityManager connectivity = (ConnectivityManager) mContext
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null)
                for (int i = 0; i < info.length; i++)
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        _isConnectionStatus = true;
                        break;
                    }
        }
    }
}
